﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Text;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using System.Drawing;

namespace HZ.Supervise.Web.Secondphase.ReportHospital
{
    public partial class hospitalAmountSubsidyStatistics2 : BasePage
    {
        DataTable dt;
        int state = 0;//显示的图片状态(0:线状,1:柱状,2:饼状)
        int sort = 0; //要显示的数据类型(0:处方单基药金额与补贴金额,1:使用基药比率  2:排名统计)
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["state"] != null)
                {
                    state = int.Parse(Request["state"]);
                }

                if (Request["sort"] != null)
                {
                    sort = int.Parse(Request["sort"]);
                }
                BindArea();
                dataBind(sort, state);
                HidSort.Value = sort.ToString();
                HidState.Value = state.ToString();
                txtHosName.Attributes.Add("readonly", "readonly");
            }
            else
            {
                sort = int.Parse(HidSort.Value);
                state = int.Parse(HidState.Value);
            }


        }

        /// <summary>    
        /// 查询数据 type:0:医院已确认补助,1:已补助
        /// </summary>
        private void dataBind(int type, int t)
        {
            HZ.Supervise.IBLL.IBLL_Prescription iPrescription = HZ.Supervise.BLLFactory.IBl_Prescription();
            StringBuilder sql = new StringBuilder();
            string sqlgroup = "";
            if (type == 0)
            {
                if (!string.IsNullOrEmpty(this.txtHosName.Text.Trim()))
                {
                    sql.Append(" and SHospitalName like '%" + this.txtHosName.Text + "%' ");
                }
                if (ddlCity.SelectedIndex != 0)
                {
                    sql.Append(" and Sareaid like '%" + ddlCity.SelectedValue.ToString().TrimEnd('0') + "%'");
                }
                if (!string.IsNullOrEmpty(txtStratTime.Text.Trim()))
                {
                    sql.Append(" and SubsidyStartTime >= '" + (new HZ.Common.ConvertHelper(txtStratTime.Text.Trim()).ToDateTime).Value.ToString() + "'");
                }
                if (!string.IsNullOrEmpty(txtEndTime.Text.Trim()))
                {
                    sql.Append(" and SubsidyEndTime <='" + (new HZ.Common.ConvertHelper(txtEndTime.Text.Trim()).ToDateTime).Value.ToString() + "'");
                }
                sql.Append(" and State = 3");
                sqlgroup = " dbo.fun_GetYearMonth(SconfirmationTime)";

                HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
                StringBuilder sqlwhere = new StringBuilder();
                sqlwhere.Append("select MAX(Sareaname) as Sareaname ,MAX(sareaid) as sareaid ,MAX(sHospitalId) as sHospitalId,MAX(SHospitalName) as SHospitalName,");
                sqlwhere.Append("dbo.fun_GetYearMonth(SconfirmationTime) as Time ,0 as temp ,SUM(Samount) as Samount,SUM(SubsidyAmount) as SubsidyAmount ,MAX(Sadduserid) as Sadduserid  from TB_Subsidy where 1=1 " + sql + " group by " + sqlgroup);

                dt = ie.FixAllMonth(iPrescription.GetSubsidyStatistics(sqlwhere.ToString()), 1, DateTime.Now.Year, "Time");
            }
            else if (type == 1)
            {
                StringBuilder sqlwhere = new StringBuilder();
                if (!string.IsNullOrEmpty(this.txtHosName.Text.Trim()))
                {
                    sqlwhere.Append(" and PHospitalName like '%" + this.txtHosName.Text + "%' ");
                }
                if (ddlCity.SelectedIndex != 0)
                {
                    if (ddlCity.SelectedIndex != 0)
                    {

                        if (ddlCity.SelectedValue.ToString().Equals("530101"))
                        {
                            sqlwhere.Append(" and PAreaId = '530100'");
                        }
                        else
                        {
                            sqlwhere.Append(" and PAreaId like '%" + ddlCity.SelectedValue.ToString().TrimEnd('0') + "%'");
                        }
                    }
                }
                if (!string.IsNullOrEmpty(txtStratTime.Text.Trim()))
                {
                    sqlwhere.Append(" and Paddtime >= '" + (new HZ.Common.ConvertHelper(txtStratTime.Text.Trim()).ToDateTime).Value.ToString() + "'");
                }
                if (!string.IsNullOrEmpty(txtEndTime.Text.Trim()))
                {
                    sqlwhere.Append(" and Paddtime <='" + (new HZ.Common.ConvertHelper(txtEndTime.Text.Trim()).ToDateTime).Value.ToString() + "'");
                }
                sql.Append("select SUM(Pamount) as Pamount, sum(Pcount) as Pcount ,PsortSource from TB_PrescriptionDetail where PrescriptionID in( select PrescriptionId from TB_Prescription where 1=1 " + sqlwhere.ToString() + ") group by PsortSource");
                dt = iPrescription.GetSubsidyStatistics(sql.ToString());
            }
            else
            {
                StringBuilder sqlwhere = new StringBuilder();
                if (!string.IsNullOrEmpty(this.txtHosName.Text.Trim()))
                {
                    sqlwhere.Append(" and PHospitalName like '%" + this.txtHosName.Text + "%' ");
                }
                if (ddlCity.SelectedIndex != 0)
                {
                    sqlwhere.Append(" and PAreaId like '%" + ddlCity.SelectedValue.ToString().TrimEnd('0') + "%'");
                }
                if (!string.IsNullOrEmpty(txtStratTime.Text.Trim()))
                {
                    sqlwhere.Append(" and Paddtime >= '" + (new HZ.Common.ConvertHelper(txtStratTime.Text.Trim()).ToDateTime).Value.ToString() + "'");
                }
                if (!string.IsNullOrEmpty(txtEndTime.Text.Trim()))
                {
                    sqlwhere.Append(" and Paddtime <='" + (new HZ.Common.ConvertHelper(txtEndTime.Text.Trim()).ToDateTime).Value.ToString() + "'");
                }
                sql.Append("select top 10 SUM(Pamount) as Pamount, sum(Pcount) as Pcount ,ProcureCatalogID,max(ProductName) as ProductName  from TB_PrescriptionDetail where PrescriptionID in( select PrescriptionId from TB_Prescription where 1=1 " + sqlwhere.ToString() + ") group by ProcureCatalogID order by Pcount desc");
                dt = iPrescription.GetSubsidyStatistics(sql.ToString());
            }

            BindChart(t);
        }
        /// <summary>
        /// 绑定地区
        /// </summary>
        public void BindArea()
        {
            IBLL_Area ia = new HZ.Supervise.BLL.BLL_Area();
            IList<Area> lstArea = new List<Area>();
            string userAreaId = CurrentAccount.GetUser().Org.Area.AreaID;
            //判断用户所属地区，如果用户是省级的 则可以列出全部区域
            //if (userAreaId == CurrentAccount.GetCurrentDeployArea().AreaID)
            //{
            int i;
            lstArea = ia.GetSonAreas(userAreaId, 1, 0, out i);
            foreach (Area area in lstArea)
            {
                ListItem ddlitem = new ListItem();
                ddlitem.Text = area.AreaName;
                ddlitem.Value = area.AreaID;
                ddlCity.Items.Add(ddlitem);
            }

            ListItem b = new ListItem();
            b.Text = "全部";
            b.Value = "";
            ddlCity.Items.Insert(0, b);


        }
        /// <summary>
        /// 图表绑定 (初始)
        /// </summary>
        private void BindChart(int type)
        {

            //空为初始页
            if (dt.Rows.Count > 0)
            {
                //string AreaName = "";//月份
                //int count = 2;//次数
                //string sort = "0";//属性 0:基药 1:非基药
                SeriesChartType t;
                switch (type)
                {
                    case 0:
                        t = SeriesChartType.Line;
                        break;
                    case 1:
                        t = SeriesChartType.Column;
                        break;
                    case 2:
                        t = SeriesChartType.Pie;
                        break;
                    default:
                        t = SeriesChartType.Column;
                        break;
                }

                //Series series1;
                //Series series2;
                //if (sort == 0)
                //{
                //    #region 图

                //    //***************** 已补贴金额 单位：万元*****************/
                //    series1 = new Series("已补贴金额 单位：万元");
                //    series1.ChartType = t;
                //    series1.BorderWidth = 1;
                //    series1.ShadowOffset = 1;

                //    //***************** 处方单金额*******************/
                //    series2 = new Series("基药总金额 单位：万元");
                //    series2.ChartType = t; //(SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                //    series2.BorderWidth = 1;
                //    series2.ShadowOffset = 1;

                //    #endregion
                //    #region 绑定值
                //    for (int i = 0; i < dt.Rows.Count; i++) //添加数据
                //    {

                //        if (dt.Rows[i]["temp"].ToString().Equals("0"))
                //        {
                //            series2.Points.AddXY(dt.Rows[i]["Time"].ToString().ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["Samount"].ToString()) / 10000).ToString());
                //            series1.Points.AddXY(dt.Rows[i]["Time"].ToString().ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["SubsidyAmount"].ToString()) / 10000).ToString());
                //        }
                //    }
                //    #endregion
                //}
                //else if (sort == 1)
                //{
                    #region 图

                    //***************** 已补贴金额 单位：万元*****************/
                    Series series1 = hospitalAmountSubsidy.Series["hospitalAmountSubsidy"];
                    series1.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Pie", true);
                    series1.BorderWidth = 1;
                    series1.ShadowOffset = 1;

                    //***************** 处方单金额*******************/
                   // series2 = new Series("总数量");
                    //series2.ChartType = SeriesChartType.Pie; ; //(SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                    //series2.BorderWidth = 1;
                    //series2.ShadowOffset = 1;

                    #endregion
                    if (dt.Rows.Count > 1)
                    {
                        series1.Points.AddXY("基本药品金额", (double.Parse(dt.Rows[0]["Pamount"].ToString()) / 10000).ToString());
                        series1.Points.AddXY("非基本药品金额", (double.Parse(dt.Rows[1]["Pamount"].ToString()) / 10000).ToString());
                        series1.Points[0].LegendText = "基本药品金额";
                        series1.Points[1].LegendText = "非基本药品金额";
                    }
                    else
                    {
                        if (int.Parse(dt.Rows[0]["PsortSource"].ToString()) == 0)
                        {
                            series1.Points.AddXY("基本药品金额", (double.Parse(dt.Rows[0]["Pamount"].ToString()) / 10000).ToString());
                            series1.Points.AddXY("非基本药品金额", 0);
                            series1.Points[0].LegendText = "基本药品金额";
                            series1.Points[1].LegendText = "非基本药品金额";
                        }
                        else
                        {
                            series1.Points.AddXY("基本药品金额", 0);
                            series1.Points.AddXY("非基本药品金额", (double.Parse(dt.Rows[1]["Pamount"].ToString()) / 10000).ToString());
                            series1.Points[0].LegendText = "基本药品金额";
                            series1.Points[1].LegendText = "非基本药品金额";
                        }


                    }

                //}
                //else
                //{
                //    #region 图

                //    //***************** 已补贴金额 单位：万元*****************/
                //    series1 = new Series("金额");
                //    series1.ChartType = t;
                //    series1.BorderWidth = 1;
                //    series1.ShadowOffset = 1;

                //    //***************** 处方单金额*******************/
                //    series2 = new Series("数量");
                //    series2.ChartType = t; //(SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                //    series2.BorderWidth = 1;
                //    series2.ShadowOffset = 1;

                //    for (int i = 0; i < dt.Rows.Count; i++) //添加数据
                //    {
                //        series1.Points.AddXY(dt.Rows[i]["ProductName"].ToString(), (double.Parse(dt.Rows[i]["Pamount"].ToString()) / 10000).ToString());
                //        series2.Points.AddXY(dt.Rows[i]["ProductName"].ToString(), (double.Parse(dt.Rows[i]["Pcount"].ToString()) / 10000).ToString());
                //    }
                //    #endregion
                //}



                #region 数据绑定到图
                //左图
                series1.IsValueShownAsLabel = false;
               // hospitalAmountSubsidy.Series["hospitalAmountSubsidy"] = series1;
                // hospitalAmountSubsidy.Series["已补贴金额 单位：万元"].ChartArea = "hospitalAmountSubsidy";
                //if (series2.Name != "总数量")
                //{
                //    series2.IsValueShownAsLabel = false;
                //    hospitalAmountSubsidy.Series.Add(series2);
                //}

                //hospitalAmountSubsidy.Series["基药总金额 单位：万元"].ChartArea = "hospitalAmountSubsidy";

                #endregion

                #region 两张图片属性
                hospitalAmountSubsidy.ChartAreas["hospitalAmountSubsidy"].AxisX.IsMarginVisible = true;
                hospitalAmountSubsidy.ChartAreas["hospitalAmountSubsidy"].Area3DStyle.Enable3D = false;
                hospitalAmountSubsidy.Series[0]["PieDrawingStyle"] = "Concave";
                hospitalAmountSubsidy.Titles.Add("基药使用率统计图");
                hospitalAmountSubsidy.Titles[0].Font = new Font("宋体", 16, FontStyle.Bold);
                hospitalAmountSubsidy.Titles[0].Position.X = 20;
                hospitalAmountSubsidy.Titles[0].Position.Y = 1;
                hospitalAmountSubsidy.Titles[0].Position.Width = 50;
                hospitalAmountSubsidy.Titles[0].Position.Height = 8;
                //hospitalAmountSubsidy.Series["hospitalAmountSubsidy"].Label = "#PERCENT{P1}";
                //if (series2.Name == "数量")
                //{
                //    hospitalAmountSubsidy.Series["金额"].YAxisType = AxisType.Secondary;
                //    hospitalAmountSubsidy.Series["数量"].YAxisType = AxisType.Primary;
                //}
                #endregion

            }
            else
            {
                //this.ChartInStorage.Visible = false;
                this.hospitalAmountSubsidy.Visible = false;
                //lblnull.Visible = true;
            }

        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            dataBind(sort, state);
        }

        protected void HzButton3_Click(object sender, EventArgs e)
        {
            HidState.Value = "2";
            state = 2;
            dataBind(sort, state);
        }

        protected void HzButton2_Click(object sender, EventArgs e)
        {

            HidState.Value = "0";
            state = 0;
            dataBind(sort, state);
        }

        protected void HzButton1_Click(object sender, EventArgs e)
        {
            HidState.Value = "1";
            state = 1;
            dataBind(sort, state);
        }

        protected void HzButton4_Click(object sender, EventArgs e)
        {

           // HidSort.Value = "0";
            sort = 0;
           // dataBind(sort, state);
            Response.Redirect("hospitalAmountSubsidyStatistics.aspx?sort=" + sort + "&state=" + 0);
        }

        protected void HzButton5_Click(object sender, EventArgs e)
        {
            HidSort.Value = "1";
            sort = 1;

            dataBind(sort, state);
        }

        protected void HzButton6_Click(object sender, EventArgs e)
        {
            //HidSort.Value = "2";
            sort = 2;
            Response.Redirect("hospitalAmountSubsidyStatistics3.aspx?sort=" + sort + "&state=" + 0);
        }

        protected void hospitalAmountSubsidy_Click(object sender, ImageMapEventArgs e)
        {
          string i =   e.PostBackValue.ToString();
        }
    }
}
